package com.huya.live.web;

import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: live
 * @description:
 * @author: Geminga
 * @create: 2019-09-19 20:41
 **/
@Component
public class WebsocketChatHandler extends TextWebSocketHandler{

    private final static List<WebSocketSession> sessions = new ArrayList<>();

    public static List<WebSocketSession> allSessions(){
        return new ArrayList<>(sessions);
    }

    /**
     * 进入房间 -- 建立连接
     */
    public void afterConnectionEstablished(WebSocketSession session) throws Exception{
        System.out.println("新用户来了"+session);
        session.getAttributes().get("userId");
        session.getAttributes().get("r  oomId");
    }
    /**
     * 退出房间 -- 连接建立
     */
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception{
        System.out.println("用户走了"+session);
    }

    /**
     * 收到消息
     * @param session
     * @param message
     * @throws IOException
     */
    public void handleTextMessage(WebSocketSession session, TextMessage message) throws IOException{
        //接收客户端发来的消息
        System.out.println("收到消息："+session+">>"+message);
        //发消息到某一个客户端
        session.sendMessage(new TextMessage("123--服务器返回的内容"));
    }
    //TODO 推送消息
}
